package com.ybkj.daijia.api.driver.v4;

import com.ybkj.daijia.api.driver.transport.ResultTransport;
import com.ybkj.daijia.api.util.ErrorConstantV4;
import com.ybkj.daijia.common.HtmlHelper;
import com.ybkj.daijia.page.Page;
import com.ybkj.daijia.server.model.Announcement;
import com.ybkj.daijia.server.model.Driver;
import com.ybkj.daijia.server.model.DriverLogin;
import com.ybkj.daijia.server.service.AnnouncementService;
import com.ybkj.daijia.server.service.DriverLoginService;
import com.ybkj.daijia.server.service.DriverService;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller("AnnouControllerV4")
@RequestMapping(value = "driver/api/Announcement/v4")
public class AnnouController {

    private static final Logger logger = LoggerFactory.getLogger(AnnouController.class);

    @Autowired
    private DriverService driverService;

    @Autowired
    private DriverLoginService driverLoginService;

    @Autowired
    private AnnouncementService announcementService;

    @RequestMapping(value = "queryAnnouncement", method = RequestMethod.GET)
    public @ResponseBody
    ResultTransport queryAnnouncement(String employToken, Integer page, Integer limit) {
        logger.debug("recv employToken:{},page:{},limit:{}", employToken, page, limit);
        if (StringUtils.isBlank(employToken) || null == page || null == limit) {
            return ResultTransport.getErrorBy(ErrorConstantV4.PARAM_ERROR);
        }

        DriverLogin driverLogin = driverLoginService.selectByToken(employToken);

        if (null == driverLogin) {
            return ResultTransport.getErrorBy(ErrorConstantV4.TOKEN_ERROR);
        }

        if (System.currentTimeMillis() >= driverLogin.getDeathDate().getTime()) {
            return ResultTransport.getErrorBy(ErrorConstantV4.TOKEN_TIME_ERROR);
        }
        Driver driver = driverService.findOne(driverLogin.getDriverId());

        if (null == driver) {
            return ResultTransport.getErrorBy(ErrorConstantV4.EMPLOY_NOT_EXISTS);
        }

        try {

            Page<Map<String, Object>> announcement = announcementService
                .queryByCompanyId(driver.getCompanyId(), page, limit);

            ResultTransport instance = ResultTransport.getErrorBy(ErrorConstantV4.SUCCESS);
            instance.setData(announcement);

            return instance;

        } catch (Exception e) {
            logger.error("api v4 driver queryAnnouncement error: {}", e);
            return ResultTransport.getErrorBy(ErrorConstantV4.SERVER_ERROR);
        }

    }

    @RequestMapping("viewAnnouncement/{id}")
    public ModelAndView edit(@PathVariable("id") Long id) {

        ModelAndView mv = new ModelAndView();

        Announcement announcement = announcementService.findOne(id);

        mv.addObject("announcement", announcement);

        mv.setViewName("driver/announcementView");

        return mv;
    }

    @RequestMapping(value = "findOneAnnouncement", method = RequestMethod.GET)
    public @ResponseBody
    Map<String, Object> findOneAnnouncement(Long announcementId) {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            Announcement announcement = announcementService.findOne(announcementId);
            map.put("code", 0);
            map.put("message", "成功");
            String html2Text = HtmlHelper.html2Text(announcement.getContent());
            announcement.setContent(html2Text);
            map.put("data", announcement);
        } catch (Exception e) {
            map.put("code", -4);
            map.put("message", "内部服务器错误");
        }
        return map;
    }
}
